Editor.Shapes.VocabProperty = function (u, i) {
	if (i == undefined) {
		i = this;
	}
	Editor.Shapes.ResourceConnectable.call(this, u, i);
}

Editor.Util.inherits(Editor.Shapes.VocabProperty, Editor.Shapes.ResourceConnectable);

Editor.Shapes.VocabProperty.prototype.resize = function (dw, dh) {
	Editor.Shapes.VocabProperty.superClass_.resize.call(this, dw, dh);
	if (this.domain) {
		Editor.STATE.SHAPES.get(this.domain).updateVocabProperties();
	}
}

Editor.Shapes.VocabProperty.prototype.select = function () {
	if (this.getDomain()) {
		Editor.Util.unselect(this);
	} else {
		Editor.Shapes.VocabProperty.superClass_.select.call(this);
	}
}

Editor.Shapes.VocabProperty.prototype.setRange = function (r) {
	this.range = r;
}

Editor.Shapes.VocabProperty.prototype.setDomain = function (d) {
	this.domain = d;
}

Editor.Shapes.VocabProperty.prototype.getRange = function () {
	return this.range;
}

Editor.Shapes.VocabProperty.prototype.getDomain = function () {
	return this.domain;
}

Editor.Shapes.VocabProperty.prototype.getTypeClass = function () {
	return "vocabProperty";
}

Editor.Shapes.VocabProperty.prototype.getInfo = function () {
	var str = '';
	if (Editor.CONFIG.DEBUG) {
		str = 'Vocab Property:\n';
		str += '---------------\n';
		if (this.range) {
			str += 'range: ' + Editor.STATE.SHAPES.get(this.range).getName() + '\n';
		}
		if (this.domain) {
			str += 'domain: ' + Editor.STATE.SHAPES.get(this.domain).getName() + '\n';
		}
		str += '---------------\n';
	}
	str += Editor.Shapes.VocabProperty.superClass_.getInfo.call(this);
	return str;
}

Editor.Shapes.VocabProperty.prototype.toObject = function () {
	return {
		'type': 'vocabProperty',
		'range': this.range,
		'resource': Editor.Shapes.VocabProperty.superClass_.toObjectResource.call(this)
	}
}

Editor.Shapes.VocabProperty.prototype.getShapeAttr = function () {
  return Editor.CONFIG.VOCABPROPERTY_SHAPE_ATTR;
}

Editor.Shapes.VocabProperty.prototype.getFigure = function () {
	return Editor.STATE.CANVAS.rect(this.getX(), this.getY(), this.getWidth(), this.getHeight(), 0).attr(this.getShapeAttr());
}

Editor.Shapes.VocabProperty.prototype.onmouseup = function () {
	// cae en una clase (c):
	// 	si esta en una clase (z) distinta a (c) 
	//		=> undo: { volver(pos.inicial); desasociar(c); asociar(z) }
	//		=> execute: { desasociar(z); asociar(c) }
	//	si esta en la misma clase (c):
	//		=> undo: { volver(pos.inicial); reordenar(c) }
	//		=> execute: { reordenar(c) }
	//	no esta en una clase:
	//		=> undo: { volver(pos.inicial); desasociar(c); }
	//		=> execute: { asociar(c) }
	// no cae en una clase:
	//	esta en una clase:
	//		=> undo: { volver(pos.inicial); asociar(c); }
	//		=> execute: { deasociar(c) }
	//	no esta en una clase:
	//		=> undo: { volver(pos.inicial); }
	//		=> execute: { }
	//		
	var impl = this.impl;
	var catched = false;

	Editor.Shapes.VocabProperty.superClass_.onmouseup.call(impl);

	Editor.STATE.SHAPES.forEach(function (pair) {
		if (!catched && !pair.value.getIsDeleted()) {
			var s = pair.value;
			if (Editor.Geometry.intersectShapes(impl.getX(), impl.getY(), impl.getWidth(), impl.getHeight(), s.getX(), s.getY(), s.getWidth(), s.getHeight())) {
				if (s.getTypeClass() == "vocabClass") {
					var oldDomain = impl.getDomain();
					var cmd = new JS.Command({
						execute: function () {
							s.addVocabPropertie(impl.getUuid());
							s.updateVocabProperties();
						},
						undo: function () {
							if(oldDomain) {
								var oldShape = Editor.STATE.SHAPES.get(oldDomain);
								oldShape.addVocabPropertie(impl.getUuid());
								oldShape.updateVocabProperties();
							} else {
								impl.setDomain(undefined);
								s.removeVocabPropertie(impl.getUuid());
							}
							s.updateVocabProperties();
						},
						stack: Editor.STATE.CMDSTACK
					});
					cmd.name = 'add vocabProperty to vocabClass';
					cmd.execute();
						
					catched = true;
				}
			}
		}
	});

	if (!catched && impl.domain) {
		var s = Editor.STATE.SHAPES.get(impl.domain);
		var cmd = new JS.Command({
			execute: function () {
				s.removeVocabPropertie(impl.getUuid());
				impl.setDomain(undefined);
				s.updateVocabProperties();
			},
			undo: function () {
				s.addVocabPropertie(impl.getUuid());
				s.updateVocabProperties();
			},
			stack: Editor.STATE.CMDSTACK
		});
		cmd.name = 'remove vocabProperty to vocabClass';
		cmd.execute();
			
		catched = true;
	}
	Editor.Util.showAllLines();
}

Editor.Shapes.VocabProperty.prototype.getText = function () {
	var t = Editor.Shapes.VocabProperty.superClass_.getText.call(this);
	if (this.getRange() && Editor.STATE.SHAPES.get(this.getRange()) != null) {
		t = t + ' :: ' + Editor.STATE.SHAPES.get(this.getRange()).getText();
	}
	return t;
}
